package cn.com.open.errorquestion.service.impl;

import cn.com.open.errorquestion.model.AnswerQuestionLog;
import cn.com.open.errorquestion.mapper.AnswerQuestionLogMapper;
import cn.com.open.errorquestion.model.MGQuestionLog;
import cn.com.open.errorquestion.model.PagerQuestion;
import cn.com.open.errorquestion.service.AnswerQuestionLogService;
import cn.com.open.errorquestion.service.ErrorQuestionService;
import cn.com.open.errorquestion.service.PagerQuestionService;
import cn.com.open.errorquestion.util.KeyWorker;
import cn.com.open.errorquestion.util.StringUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
@Transactional(rollbackFor = RuntimeException.class)
public class AnswerQuestionLogServiceImpl implements AnswerQuestionLogService {

    private static Logger logger = LoggerFactory.getLogger(AnswerQuestionLogServiceImpl.class);


    @Autowired
    private AnswerQuestionLogMapper answerQuestionLogMapper;

    @Autowired
    private PagerQuestionService pagerQuestionService;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存作答记录
     * @param answerData
     */
    @Override
    public void save(AnswerQuestionLog answerData) {

        answerQuestionLogMapper.save(answerData);
    }

    /**
     * 1，保存作答记录
     * 2，记录错题记录
     * @param answerData
     */
    @Override
//    @Async
    public void saveQuestion(AnswerQuestionLog answerData) throws RuntimeException {
        long snowflakeId = KeyWorker.nextId();
        logger.info("线程标识="+snowflakeId);


        answerData.setId(snowflakeId);
        //分表策略   确定存储到哪张表
        String examineeId = answerData.getExamineeId();
        int tableNumber = Math.abs(examineeId.hashCode() % 5);
        answerData.setTableNumber(tableNumber);

        AnswerQuestionLog answerQuestionLog = this.findOne(answerData);

        if(!StringUtil.isEmpty(answerData.getPaperId())){
            PagerQuestion pagerQuestion = new PagerQuestion(snowflakeId,answerData.getPaperId(), answerData.getQuestionId());
            PagerQuestion findPagerQuestion = pagerQuestionService.findOne(pagerQuestion);
            if(StringUtils.isEmpty(findPagerQuestion)){
                pagerQuestionService.save(pagerQuestion);
            }
        }


        if(StringUtils.isEmpty(answerQuestionLog)){
            //错误
            if(answerData.getAnswerStates() == 0){
                answerData.setFalseCount(1);
            }else if(answerData.getAnswerStates() == 1){
                answerData.setTrueCount(1);
            }

            // 保存作答记录
            this.save(answerData);
        }else {
            //错误
            if(answerData.getAnswerStates() == 0){
                answerQuestionLog.setFalseCount(1);
            }else if(answerData.getAnswerStates() == 1){
                answerQuestionLog.setTrueCount(1);
            }
            // 修改作答记录
            this.update(answerQuestionLog);
        }

        mongoTemplate.insert(JSON.parseObject(answerData.getContentJson()),"mgquestionlog");
    }

    /**
     * 修改
     * @param answerData
     */
    @Override
    public void update(AnswerQuestionLog answerData) {
        answerQuestionLogMapper.update(answerData);
    }

    /**
     * 查询作答记录是否存在
     * @return
     */
    @Override
    public AnswerQuestionLog findOne(AnswerQuestionLog answerData)  {
        return answerQuestionLogMapper.findOne(answerData) ;
    }

    @Override
    public void saveQuestion(List<AnswerQuestionLog> answerDatas) {
        for ( AnswerQuestionLog answerData: answerDatas) {
            this.saveQuestion(answerData);
        }
    }
}
